package com.compomics.peptizer.gui.component;

import com.compomics.peptizer.gui.PeptizerGUI;
import com.compomics.peptizer.gui.listener.AddAgentFilterActionListener;
import com.compomics.peptizer.gui.listener.AddSequenceFilterActionListener;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * This class is a
 */
public class MainIconPanel {
    private JButton btnSaveTask;
    private JButton btnNewTask;
    private JToggleButton btnInfo;
    private JPanel jpanContent;
    private JToggleButton btnSerSave;
    private JToggleButton btnSerLoad;
    private JToggleButton btnSeqFind;
    private JToggleButton btnHelp;
    private JToggleButton btnAgentFilter;
    private PeptizerGUI iPeptizerGUI;

    /**
     * Constructs a panel has a series of application wide functions.
     */
    public MainIconPanel(PeptizerGUI aPeptizerGUI) {
        iPeptizerGUI = aPeptizerGUI;

        // Start new task.
        btnNewTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.newTask();
            }
        });
        btnNewTask.setToolTipText("New peptizer task");

        // Save Current task.
        btnSaveTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.saveTask();
            }
        });
        btnSaveTask.setToolTipText("Save peptizer task");

        btnInfo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.toggleInfoPanel();
            }
        });
        btnInfo.setToolTipText("Toggle logging information");

        btnSerSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.saveSerializedIdentifications();

            }
        });

        btnSerLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.loadSerializedPeptideIdentifications();
            }
        });

        btnHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent aActionEvent) {
                iPeptizerGUI.showAbout();
            }
        });

        btnSeqFind.addActionListener(new AddSequenceFilterActionListener(iPeptizerGUI));

        btnAgentFilter.addActionListener(new AddAgentFilterActionListener(iPeptizerGUI));

        EmptyBorder lBorder = new EmptyBorder(0, 0, 0, 0);

        btnInfo.setBorder(lBorder);
        btnNewTask.setBorder(lBorder);
        btnSaveTask.setBorder(lBorder);
        btnSerSave.setBorder(lBorder);
        btnSerLoad.setBorder(lBorder);
        btnHelp.setBorder(lBorder);
        btnSeqFind.setBorder(lBorder);
        btnAgentFilter.setBorder(lBorder);

    }


    private void createUIComponents() {
        // TODO: place custom component creation code here
    }

    /**
     * If TRUE; then inactivate the buttons that are not needed at start time.
     * if FALSE; enable all buttons.
     */
    public void showAll(boolean aShowAll) {
        // Inverse the boo
        btnAgentFilter.setEnabled(aShowAll);

        btnSeqFind.setEnabled(aShowAll);

        btnSerSave.setEnabled(aShowAll);
        btnSaveTask.setEnabled(aShowAll);

    }

    /**
     * Inactivate the buttons that are not needed at start time.
     */
    private void setStartupMode() {
        btnAgentFilter.setEnabled(false);

        btnSeqFind.setEnabled(false);

        btnSerSave.setEnabled(false);
        btnSaveTask.setEnabled(false);

    }


    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        jpanContent = new JPanel();
        jpanContent.setLayout(new FormLayout("fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow,left:4dlu:noGrow,fill:50px:noGrow", "center:10px:noGrow,top:3dlu:noGrow,center:d:noGrow"));
        btnNewTask = new JButton();
        btnNewTask.setIcon(new ImageIcon(getClass().getResource("/image/ICON_newtask.png")));
        btnNewTask.setText("");
        CellConstraints cc = new CellConstraints();
        jpanContent.add(btnNewTask, cc.xy(1, 3));
        btnSaveTask = new JButton();
        btnSaveTask.setEnabled(true);
        btnSaveTask.setIcon(new ImageIcon(getClass().getResource("/image/ICON_save_project.png")));
        btnSaveTask.setText("");
        jpanContent.add(btnSaveTask, cc.xy(3, 3));
        btnInfo = new JToggleButton();
        btnInfo.setHideActionText(true);
        btnInfo.setIcon(new ImageIcon(getClass().getResource("/image/ICON_log.png")));
        btnInfo.setText("");
        jpanContent.add(btnInfo, cc.xy(5, 3));
        btnSerSave = new JToggleButton();
        btnSerSave.setHideActionText(true);
        btnSerSave.setIcon(new ImageIcon(getClass().getResource("/image/ICON_save_ser.png")));
        btnSerSave.setText("");
        jpanContent.add(btnSerSave, cc.xy(7, 3));
        btnSerLoad = new JToggleButton();
        btnSerLoad.setHideActionText(true);
        btnSerLoad.setIcon(new ImageIcon(getClass().getResource("/image/ICON_load_ser.png")));
        btnSerLoad.setText("");
        jpanContent.add(btnSerLoad, cc.xy(9, 3));
        btnSeqFind = new JToggleButton();
        btnSeqFind.setHideActionText(true);
        btnSeqFind.setIcon(new ImageIcon(getClass().getResource("/image/ICON_find.png")));
        btnSeqFind.setText("");
        jpanContent.add(btnSeqFind, cc.xy(11, 3));
        btnAgentFilter = new JToggleButton();
        btnAgentFilter.setHideActionText(true);
        btnAgentFilter.setIcon(new ImageIcon(getClass().getResource("/image/ICON_filter.png")));
        btnAgentFilter.setText("");
        jpanContent.add(btnAgentFilter, cc.xy(13, 3));
        btnHelp = new JToggleButton();
        btnHelp.setHideActionText(true);
        btnHelp.setIcon(new ImageIcon(getClass().getResource("/image/ICON_help.png")));
        btnHelp.setText("");
        jpanContent.add(btnHelp, cc.xy(15, 3));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }
}
